package cn.com.infcn.monitor.task.impl;

import org.apache.mina.core.session.IoSession;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.SigarException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCOperatingSystem;
import cn.com.infcn.monitor.bean.IFCResponseBean;
import cn.com.infcn.monitor.util.IFCBeanUtils;
import cn.com.infcn.monitor.util.IFCMinitorConstanst;

/**
 * 获取操作系统相关信息
 * 
 * @author jjs
 *
 */
public class OsTask extends BaseTask {

	private final static Logger logger = LoggerFactory.getLogger(OsTask.class);

	public OsTask(IoSession session, Object message) {
		super(session, message);
	}

	@Override
	public void execute() throws Exception {
		IFCResponseBean result = os();
		sendResponse(result);
	}

	private IFCResponseBean os() throws SigarException {
		IFCResponseBean result = new IFCResponseBean();

		try {
			OperatingSystem os = OperatingSystem.getInstance();
			IFCOperatingSystem operatingSystem = new IFCOperatingSystem();
			IFCBeanUtils.copyProperties(os, operatingSystem);

			result.setResult(operatingSystem);
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg(e.getMessage());
		}
		return result;
	}

}
